﻿

using System.Collections.Generic;
using System.Data;
using Channelvn.Cached;
using Core.Library;

namespace DataObject.News
{
    public class NewsCache
    {
        public static DataTable News_GetHighlightHomepage(int top)
        {
            var listParams = new object[] { top };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("News_GetHighlightHomepage", listParams);
            var fromcache = CacheController.GetCache(Constants.LIST_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = NewsController.News_GetHighlightHomepage(top))
                {
                    CacheController.Add(Constants.LIST_CACHE_CATEGORY, cacheName, dt, Constants._15min);
                    return dt;
                }
            }
        }

        public static DataTable News_GetHighlightList(int top)
        {
            var listParams = new object[] { top };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("News_GetHighlightList", listParams);
            var fromcache = CacheController.GetCache(Constants.LIST_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = NewsController.News_GetHighlightList(top))
                {
                    CacheController.Add(Constants.LIST_CACHE_CATEGORY, cacheName, dt, Constants._15min);
                    return dt;
                }
            }
        }

        public static DataTable News_GetListPaging(int pageIndex, int pageSize, List<long> exceptionId)
        {
            var listParams = new object[] {pageIndex, pageSize, exceptionId };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("News_GetListPaging", listParams);
            var fromcache = CacheController.GetCache(Constants.LIST_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = NewsController.News_GetListPaging(pageIndex, pageSize, exceptionId))
                {
                    CacheController.Add(Constants.LIST_CACHE_CATEGORY, cacheName, dt, Constants._15min);
                    return dt;
                }
            }
        }

        public static int News_Count(List<long> exceptionId)
        {
            var listParams = new object[] { exceptionId };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("News_Count", listParams);
            var fromcache = CacheController.GetCache(Constants.LIST_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (int)fromcache;
            }
            else
            {
                int r = NewsController.News_Count(exceptionId);
                CacheController.Add(Constants.LIST_CACHE_CATEGORY, cacheName, r, Constants._15min);
                return r;
            }
        }

        public static DataTable News_GetOtherNews(int pageIndex, int pageSize, List<long> exceptionId)
        {
            var listParams = new object[] { pageIndex, pageSize, exceptionId };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("News_GetOtherNews", listParams);
            var fromcache = CacheController.GetCache(Constants.LIST_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = NewsController.News_GetOtherNews(pageIndex, pageSize, exceptionId))
                {
                    CacheController.Add(Constants.LIST_CACHE_CATEGORY, cacheName, dt, Constants._15min);
                    return dt;
                }
            }
        }

        public static DataTable News_GetDetails(long id)
        {
            var listParams = new object[] { id };
            var cacheName = CacheHelper.CacheKey.GetCacheKey("News_GetDetails", listParams);
            var fromcache = CacheController.GetCache(Constants.DETAILS_CACHE_CATEGORY, cacheName);
            if (fromcache != null)
            {
                return (DataTable)fromcache;
            }
            else
            {
                using (var dt = NewsController.News_GetDetails(id))
                {
                    CacheController.Add(Constants.DETAILS_CACHE_CATEGORY, cacheName, dt, Constants._15min);
                    return dt;
                }
            }
        }
    }
}
